// src/api/index.js
import axios from 'axios';
import {message} from "ant-design-vue";
import router from '../router/index.js'

const service = axios.create({
    baseURL: '', // 替换为实际的 API 基础地址
    timeout: 10000
});
// 请求拦截器
service.interceptors.request.use(
    config => {
        let token = localStorage.getItem('h5-token');
        if (token) {
            config.headers['x-token'] = token;
        }
        // 可以在这里添加请求头信息，例如 token
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

// 响应拦截器
service.interceptors.response.use(
    response => {
        const res = response.data;
        if (res.code === 402) {
            message.warning(res.message)
            localStorage.removeItem('h5-token');
            router.push('/login')
        }
        if (res.code === 403) {
            message.warning(res.message)
        }
        return res;
    },
    error => {
        console.log('err' + error);
        return Promise.reject(error);
    }
);

export default service;
